﻿#region 命名空间

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using Sharp.Domain.PlatformBoundedContext.PermissionModule.Aggregates;

#endregion

namespace Sharp.Domain.PlatformBoundedContext.ModelConfigurations
{
    public partial class PermissionTypeConfiguration
        : EntityTypeConfiguration<Permission>
    {
        public PermissionTypeConfiguration()
        {
            // keys
            HasKey(t => t.Id);

            // Properties
            Property(t => t.Id)
                .IsRequired()
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            Property(t => t.Code)
                .HasMaxLength(50)
                .IsRequired();
            Property(t => t.Title)
                .HasMaxLength(50)
                .IsRequired();
            Property(t => t.Category)
                .HasMaxLength(20)
                .IsRequired();

            Property(t => t.ParentId)
                .IsOptional();

            Property(t => t.OrderIndex)
                .IsRequired();

            Property(t => t.Route)
                .IsUnicode(false).HasMaxLength(255)
                .IsOptional();


            // Relationships
            HasMany(t => t.Roles)
                .WithMany(t => t.Permissions)
                .Map(m =>
                {
                    m.ToTable("Platform_PermissionRole");
                    m.MapLeftKey("PermissionId");
                    m.MapRightKey("RoleId");
                });

            HasMany(t => t.Users)
                .WithMany(t => t.Permissions)
                .Map(m =>
                {
                    m.ToTable("Platform_PermissionUser");
                    m.MapLeftKey("PermissionId");
                    m.MapRightKey("UserId");
                });
        }
    }
}